package com.atfusu.controller;

import com.atfusu.bean.entity.Admin;
import com.atfusu.bean.entity.Permission;
import com.atfusu.service.AdminService;
import com.atfusu.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/25
 * Description:
 **/
@Controller
public class IndexController {

    private static final String PAGE_INDEX = "frame/index";
    private static final String PAGE_MAIN = "frame/main";

    @DubboReference
    private AdminService adminService;

    @DubboReference
    private PermissionService permissionService;

    @RequestMapping("/")
    public String index(Map map) {
        //根据登录人的权限进行左侧菜单栏显示
//        Long adminId = 28L;
//        Admin admin = adminService.getById(adminId);
        //获取登录人信息（spring Security）
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Admin admin = adminService.getAdminByUsername(user.getUsername());
        List<Permission> permissionList = permissionService.getPermissionByAdmin(admin.getId());
        map.put("admin", admin);
        map.put("permissionList", permissionList);
        return PAGE_INDEX;
    }

    @RequestMapping("/main")
    public String main() {

        return PAGE_MAIN;
    }

    @RequestMapping("/login")
    public String login() {

        return "frame/login";
    }

    @RequestMapping("/auth")
    public String auth(){

        return "frame/auth";
    }
}
